package com.example.aaaexemplos;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

public class CadastrarTask extends AsyncTask<Void, Integer, Void> {

	private Context context;
	private ProgressDialog progress;
	private CadastrarCallback callback;
	private boolean sucess;
	private Person person;

	public CadastrarTask(Context context, CadastrarCallback callback,
			Person person) {
		this.context = context;
		this.callback = callback;
		this.person = person;
	}

	@Override
	protected void onPreExecute() {
		this.progress = ProgressDialog.show(this.context,
				this.context.getString(R.string.app_name),
				this.context.getString(R.string.loading));
	}

	@Override
	protected Void doInBackground(Void... params) {

		// sommente pra demorar
		this.sucess = true;
		try {
			Thread.sleep(3000);
			DAO dao = new DAO(this.context);
			dao.save(this.person);
		} catch (InterruptedException e) {
			this.sucess = false;
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		this.progress.hide();
		this.sucess = false;

		if (this.sucess) {
			this.callback.onAddSuccess();
		} else {
			this.callback.onAddFailure();
		}
	}
}
